package eu.dominicum.ft.pd;

import eu.dominicum.ft.pd.mao.MakePersonMAO;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.ValidationException;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.Set;

/**
 * A natural person.
 *
 * @author Harry Klerks
 * @since 2013M1
 */
public class Person extends PersistentDomainObject {

    /**
     * Name.
     */
    private String name;

    /**
     * Nickname.
     */
    private String nickname;

    /**
     * Name prefix.
     */
    private String namePrefix;

    /**
     * Make a Person.
     *
     * @param makePersonMAO -
     * @return Person
     */
    public static Person make(final MakePersonMAO makePersonMAO) {

        ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
        Validator validator = validatorFactory.getValidator();
        Set<ConstraintViolation<MakePersonMAO>> violations = validator.validate(makePersonMAO);

        if (!violations.isEmpty()) {
            throw new ValidationException((violations.iterator().next().getMessage()));
        }

        Person newPerson = new Person();
        newPerson.namePrefix = makePersonMAO.getNamePrefix();
        newPerson.name = makePersonMAO.getName();
        newPerson.nickname = makePersonMAO.getNickname();

        return newPerson;
    }

    /**
     * .
     *
     * @return String
     */

    public final String getName() {
        return name;
    }

    /**
     * .
     *
     * @return String
     */
    public final String getNickname() {
        return nickname;
    }

    /**
     * .
     *
     * @return String
     */
    public final String getNamePrefix() {
        return namePrefix;
    }

    /**
     * Return the fullname of this Person.
     *
     * @return String
     */
    public final String getFullname() {

        StringBuilder stringBuilder = new StringBuilder();

        if (this.nickname != null) {
            stringBuilder.append(this.nickname);
            stringBuilder.append(" ");
        }

        if (this.namePrefix != null) {
            stringBuilder.append(this.namePrefix);
            stringBuilder.append(" ");
        }

        stringBuilder.append(this.name);

        return stringBuilder.toString();
    }

    /**
     * Return the name as it should appear in ordered lists.
     *
     * @return String
     */
    public final String getOrderedName() {

        StringBuilder stringBuilder = new StringBuilder(this.name);
        stringBuilder.append(", ");

        if (this.nickname != null) {
            stringBuilder.append(this.nickname);
            stringBuilder.append(" ");
        }

        if (this.namePrefix != null) {
            stringBuilder.append(this.namePrefix);
            stringBuilder.append(" ");
        }

        return stringBuilder.toString();
    }
}
